package com.yoomiito.app.ui.anyview.goodsinfo.header

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.view.View
import android.view.ViewStub
import android.widget.LinearLayout
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.isEmptyOr0
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.removeDecimalPoint
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.item_coupon_view.view.*
import kotlinx.android.synthetic.main.item_goods_detail_price_2.view.*
import kotlinx.android.synthetic.main.video_play_view.view.*
import kotlinx.android.synthetic.main.view_goodsinfo_header.view.*

class SddVipGoodsInfoHeaderView(context: BaseActivity<*>) : BaseGoodsInfoHeaderView(context) {

    override fun getLayoutId(): Int = R.layout.view_goodsinfo_header

    override fun getParentView(): LinearLayout = this

    override fun initView(goodsDetail: GoodsDetail) {
        initBanner(banner, goodsDetail)
        getVideoTime(goodsDetail) {
            playVideoView.visibility = View.VISIBLE
            durationTv.text = DateUtils.ms2min(it.duration)
            playVideoView.setOnClickListener { _ -> OnVideoClickListener(it) }
        }
        goodsDesTv.visibility = View.GONE
        if (viewStubIsInflate(R.id.priceLlVs)){
            priceLlVs.layoutResource = R.layout.item_goods_detail_price_2
            priceLlVs.inflate()
        }


        //价格
        val priceT = goodsDetail.salePrice.removeDecimalPoint()
        val oldPriceT = goodsDetail.reserve_price.removeDecimalPoint()
        val priceStr = if (oldPriceT.isEmptyOr0()) "¥$priceT " else "¥$priceT ¥$oldPriceT"
        val spannableString = SpannableString(priceStr)
        spannableString.setSpan(RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(RelativeSizeSpan(0.65f), (if (priceT.contains(".")) priceT.indexOf(".") else priceT.length) + 1, priceT.length + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(RelativeSizeSpan(0.37f), priceT.length + 1, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), priceT.length + 2, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(StrikethroughSpan(), priceT.length + 2, priceStr.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        price.text = spannableString

        //送优米 赚
        //赚显示规则：未登录不显示；会员不显示  粉丝及普通VIP显示后台配置的基础佣金；VIP及VIP以上显示【7*佣金倍数】
        awardView.visibility = if (!App.mAppConfig.isLogin || UserInfoConfig.getUser().three_role_id.toInt() < Type.LevelType.SDD_VIP_10 || SpannableUtils.isEmptyOr0(goodsDetail.get_money)) View.GONE else View.VISIBLE
        awardView.setText("¥${goodsDetail.get_money.removeDecimalPoint()}")
        //优米 有就显示
        couponView.visibility = View.GONE
//        couponView.setTextSize(12f)
//        couponView.setText("${goodsDetail.yo_money}优米")
//        couponView.setHeaderText("送")
//        couponView.visibility = if (goodsDetail.yo_money==0) View.GONE else View.VISIBLE
//        couponView.setColor(ResUtil.getColor(R.color.color_FF7D00),ResUtil.getColor(R.color.color_1AFF7D00))

        //升级提示显示规则，未登录不显示；会员及会员以上VIP以下身份显示；VIP及VIP以上不显示。升级后奖励显示【7*佣金倍数】
//        if (App.mAppConfig.isLogin&&UserInfoConfig.getRoleId() <Type.LevelType.VIP_10){
//            val v = upgradeVipVs.inflate()
//            val lp1 =v.layoutParams as LinearLayout.LayoutParams
//            lp1.topMargin = DensityUtil.dp2px(14f)
//            tv.text = "升级Vip，推荐购买本商品可奖"
//            VIPAwardTv.text = "¥${goodsDetail.upgrade_money.removeDecimalPoint()}"
//            upgradeVip.setOnClickListener { RouterUtils.toMain(context as NewGoodsInfoActivity,1) }
//        }
        //标题
        val titleTvLp = goodsTitleTv.layoutParams as LayoutParams
        titleTvLp.topMargin = DensityUtil.dp2px(12f)
        goodsTitleTv.text = goodsDetail.title
        goodsTitleCopy(goodsTitleTv, goodsDetail.title)
        //销量 及分享件数
        val size = "已抢${goodsDetail.volume}件"
        val sizeSpannableString = SpannableString(size)
        sizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, size.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//销量颜色
        goodsSaleSizeTv.text = sizeSpannableString

        val shareSize = "分享${goodsDetail.share_num}次"
        val shareSizeSpannableString = SpannableString(shareSize)
        shareSizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, shareSize.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//分享件数颜色
        goodsShareSizeTv.text = shareSizeSpannableString

        if (viewStubIsInflate(R.id.itemGoodsVs)){
            itemGoodsVs.layoutResource = R.layout.item_goods_flag_1
            itemGoodsVs.inflate()
        }
    }

}